const app = getApp();
let ServiceUrl = app.globalData.ServiceUrl;//统一域名
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');//引入腾讯地图插件
var qqmapsdk = new QQMapWX({
  key: '66XBZ-6EWK2-YZ7UP-CPLDJ-XQLUK-TTFJ6'//腾讯地图API
});
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude: '',//经度
    longitude: '',//纬度
    active:'0',
    buttonText:'',
    order_id:'',
    order_sn:'',
    user_id:'',
    oldman_phone:'',
    steps: [
      {
        text: '[已经接单]',
        desc: '',
      },
      {
        text: '[开始服务]',
        desc: '',
      },
      {
        text: '[到达医院]',
        desc: '',
      },
      {
        text: '[提交就医材料]',
        desc: '',
      },
      {
        text: '[离开]',
        desc: '',
      },
      {
        text: '[结束服务]',
        desc: '',
      },
    ],
    show: false,//隐藏
    imgUrl: ''
  },
  
  //取消上传图片
  closeImg:function(){
    this.setData({
      show:false
    })
  },
  
  uploadImage: function(){
    let that = this
    wx.showLoading({
      title: '上传中...',
    })
    wx.request({
      url: ServiceUrl + 'uploadAdd',
      data: {
            order_id:that.data.order_id,
            latitude: that.data.latitude,
            longitude: that.data.longitude,
//            user_id: that.data.user_id,
            imgUrl: that.data.imgUpload
      },
      method: "post",
      success: function(res){
        console.log(res)
        if(res.data.state == 200){
          wx.hideLoading();
          that.setData({
            show: false,
            buttonText: res.data.buttonName,
          })
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
          //调起require函数刷新页面
          let order_id = that.data.order_id;
          that.require(order_id);
        }else{
          wx.showModal({
            title:"提示",
            content:res.data.msg,
            showCancel: false,
            confirmColor: 'skyblue',
            success: function(res){
              if(res.confirm){
                that.setData({show: false})
              }
            }
          })
        }
      },
      fail:function(){
        console.log('发送请求失败')
      }
    })
  },
  //上传图片
  update:function(){
    let that = this
    //that.setData({show: true})
    wx.chooseImage({
      success (res) {
        const tempFilePaths = res.tempFilePaths
        wx.showLoading({
          title: '压缩图片中',
        })
        wx.uploadFile({
          url: ServiceUrl + 'upload', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            test: 'file'
          },
          success (res){
//            console.log(tempFilePaths[0])
//            console.log(res.data)
            console.log(JSON.parse(res.data))
            let data = JSON.parse(res.data);
            if(data.state == 200){
              wx.hideLoading({
                success: (res) => {
                  that.setData({
                    imgUpload: data.imgUrl,//返回来的上传地址
                    imgUrl: tempFilePaths[0],//临时地址
                    show: true
                  })
                },
              })
            }else{
              wx.showModal({
                title:"提示",
                content:res.data.msg,
                showCancel: false,
                confirmColor: 'skyblue',
                success: function(){
                  console.log('刷新页面')
                }
              })
            }
          }
        })
      }
    })
  },
 /**
  * 调起电话，就诊对象的电话
  */
 callPhone: function(){
   wx.makePhoneCall({
     phoneNumber: this.data.oldman_phone
   })
 },
  //操作步骤
  startService:function(){
    let that = this
//    console.log(that.data.order_id)
    if(that.data.buttonText == '订单已完成'){
      return false;
    }
    wx.request({
      url: ServiceUrl + 'serviceProgress',
      data: {
        order_id: that.data.order_id,
        latitude: that.data.latitude,
        longitude: that.data.longitude
      },
      method: "post",
      success: function(res){
        console.log(res)
        if(res.data.state == 200){
          that.setData({buttonText: res.data.data})
          //调起require函数刷新页面
          let order_id = that.data.order_id;
          that.require(order_id);
          wx.showToast({
            title: res.data.msg,
          })
        }else if(res.data.state == 201){
          //此时状态为提交就医材料
          that.update();
        }else if(res.data.state == 202){
          wx.showToast({
            title: res.data.msg,
          })
          setTimeout(() => {
            wx.redirectTo({
              url: '../index/index',
            })
          }, 2000);
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      },
      fail: function(){
        console.log('发送请求失败')
      }
    })
  },
  /**
   * 更新当前定位
   */
  confirmPostation:function(){
    let that = this
    wx.getLocation({
      type: 'gcj02',
      success (res) {
        console.log(res)
        if(res.errMsg=="getLocation:ok"){
          that.setData({
            latitude: res.latitude,
            longitude:res.longitude
          });
//          console.log(that.data.latitude)
          //console.log('获取定位成功')
        }else{
          console.log('获取定位失败')
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let that = this
    that.setData({
      order_id: options.order_id,
      user_id: options.user_id
    })
//    that.setData({order_id: "20201110985652",user_id:2})
    //调起require函数
    let order_id = options.order_id;
    that.require(order_id);
    //更新坐标
    that.confirmPostation();
  },
  require:function(order_id){
    let that = this
    wx.request({
      url: ServiceUrl + 'startOrderInfo',
      data: {
         order_id: order_id,
         user_id: that.data.user_id
//        order_id: '20201110985652',
//        user_id:2
      },
      method: "get",
      success: function(res){
        console.log(res)
        let src_type = res.data.data.src_type==0?'微信下单':res.data.data.src_type==1?'后台下单':'其他';
        let active = res.data.progress.length - 1
        if(res.data.state == 200){
          that.setData({
            order_sn: res.data.data.order_sn,
            oldman_name: res.data.data.oldman_name,
            start_name: res.data.data.start_name,
            to_name: res.data.data.to_name,
            require_date: res.data.data.require_date,
            require_time: res.data.data.require_time,
            order_state: res.data.data.order_state,
            src_type: src_type,
            order_id: res.data.data.order_id,
            distance: res.data.data.distance,
            pay_fee: res.data.data.pay_fee,
            active: active,
            buttonText:res.data.data.buttonText,
            oldman_phone: res.data.data.oldman_phone
          })
          //更新状态
          let progress = res.data.progress;
          let i = res.data.progress.length;
          var start_name = '';
          var to_name='';
          for(i in that.data.steps){
            for(i in progress){
              // 用setData修改数组或对象中的一个属性值
              var desc = "steps[" + i + "].desc";
              var text = "steps[" + i + "].text";
              if(i == 1){
                start_name = res.data.data.start_name
                that.setData({
                  [text] : "["+progress[i].state_name+"]："+start_name,
                  [desc] : progress[i].add_time
                })
              }else if(i == 2){
                to_name = res.data.data.to_name
                that.setData({
                  [text] : "["+progress[i].state_name+"]："+to_name,
                  [desc] : progress[i].add_time
                })
              }else{
                that.setData({
                  [text] : "["+progress[i].state_name+"]",
                  [desc] : progress[i].add_time
                })
              }
            }
          }
        }
      },
      fail: function(){
        console.log('发送请求失败')
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that=this;
    that.require(that.data.order_id);
    that.confirmPostation();
    setTimeout(() => {
      wx.stopPullDownRefresh();//停止下拉刷新
    }, 3000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})